package com.bto.solar.work.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bto.solar.work.aliyunsms.SmsService;
import com.bto.solar.work.common.R;
import com.bto.solar.work.constant.Constants;
import com.bto.solar.work.dto.CreateUserReq;
import com.bto.solar.work.dto.QueryUserReq;
import com.bto.solar.work.dto.RegisterUserReq;
import com.bto.solar.work.dto.UpdatePasswordReq;
import com.bto.solar.work.entity.User;
import com.bto.solar.work.service.BtoUserService;
import com.bto.solar.work.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author adrian
 * @since 2023-08-09
 */
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private BtoUserService btoUserService;
    @Autowired
    private SmsService smsService;

    /**
     * 测试-批量新增用户
     */
//    @PostMapping("/add-list")
//    public R<String> addUser(@Valid @RequestBody CreateUserListReq createUserListReq) {
//        for (CreateUserReq createUserReq : createUserListReq.getCreateUserReqList()) {
//            boolean add = btoUserService.addUser(createUserReq);
//            if (!add) {
//                return R.fail(null, "用户新增失败");
//            }
//        }
//        return R.ok(null, "用户新增成功");
//    }

    /**
     * 用户新增
     */
    @PostMapping("/add")
    public R<String> addUser(@Valid @RequestBody CreateUserReq createUserReq) {
        boolean add = btoUserService.addUser(createUserReq);
        if (add) {
            return R.ok(null, "用户新增成功");
        } else {
            return R.fail(null, "用户新增失败");
        }
    }

    /**
     * 用户注册
     */
    @PostMapping("/register")
    public R<String> register(@Valid @RequestBody RegisterUserReq registerUserReq) {
        btoUserService.registerUser(registerUserReq);
        return R.ok("注册成功！等待审核");
    }

    /**
     * 用户审核
     */
    @PostMapping("/approve")
    public R<String> approve(
            @RequestParam(value = "userId") Long userId,
            @RequestParam(value = "approve") String approve
    ) {
        btoUserService.approveUser(userId, approve);
        return R.ok("审核成功！");
    }

    /**
     * 用户编辑
     */
    @PutMapping("/update")
    public R<String> updateUser(@Valid @RequestBody CreateUserReq createUserReq) {
        boolean update = btoUserService.updateUser(createUserReq);
        if (update) {
            return R.ok(null, "用户修改成功");
        } else {
            return R.fail(null, "用户修改失败");
        }
    }

    /**
     * 用户查询
     */
    @GetMapping("/infos")
    public R<Page<UserVO>> getUserPage(
            @RequestParam(value = "currentPage", required = false, defaultValue = "1") Integer currentPage,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
            @RequestParam(value = "companyId", required = false) Long companyId,
            @RequestParam(value = "status", required = false) Byte status,
            @RequestParam(value = "categoryId", required = false) String categoryId,
            @RequestParam(value = "queryValue", required = false) String queryValue,
            @RequestParam(value = "queryUserId", required = false) Long queryUserId,
            @RequestParam(value = "approve") String approve,
            @RequestParam(value = "orderBy", required = false, defaultValue = "true") boolean orderBy
    ) {

        Page<UserVO> userVoPage = btoUserService.getUserInfosByPage(currentPage, pageSize, companyId,
                status, categoryId, queryValue, queryUserId, approve, orderBy);
        return R.ok(userVoPage);
    }

    /**
     * 用户查询
     */
    @GetMapping("/body/infos")
    public R<Page<UserVO>> getUserPage(@RequestBody QueryUserReq queryUserReq) {
        Page<UserVO> userVoPage = btoUserService.getUserInfosByPage(queryUserReq);
        return R.ok(userVoPage);
    }

    /**
     * 个人中心
     */
    @GetMapping("/my-user")
    public R<UserVO> getUser() {
        UserVO userVO = btoUserService.getUserInfo(null);
        return R.ok(userVO);
    }

    /**
     * 重置密码
     */
    @PostMapping("/reset-password")
    public R<String> resetPassword(
            @RequestParam(value = "userId") Long userId
    ) {
        boolean reset = btoUserService.resetPassword(userId);
        if (reset) {
            return R.ok(null, "密码重置成功！");
        } else {
            return R.fail(null, "密码重置失败！");
        }
    }

    /**
     * 修改密码的验证码
     */
    @PostMapping("/send-password-sms")
    public R<String> getSmsCode (@RequestParam(value = "phone") String phone) {
        //校验手机号
        btoUserService.isUserByPhone(phone);
        smsService.getSmsCode(phone, Constants.PASSWORD);
        return R.ok("验证码已发送！验证码有效为5分钟");
    }


    /**
     * 修改密码
     */
    @PostMapping("/update-password")
    public R<String> updatePassword (@Valid @RequestBody UpdatePasswordReq updatePasswordReq) {
        btoUserService.updatePassword(updatePasswordReq);
        return R.ok("修改密码成功");
    }


    /**
     * 修改用户状态
     */
    @PostMapping("/update-status")
    public R<String> updateStatus(
            @RequestParam(value = "userId") Long userId,
            @RequestParam(value = "status") Byte status
    ) {
        boolean update = btoUserService.updateStatus(userId, status);
        if (update) {
            return R.ok(null, "用户状态修改成功！");
        } else {
            return R.fail(null, "用户状态修改失败！");
        }
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/delete/{userId}")
    public R<String> delete(@PathVariable("userId") Long userId) {
        boolean delete = btoUserService.deleteUser(userId);
        if (delete) {
            return R.ok(null, "用户删除成功！");
        } else {
            return R.fail(null, "用户删除失败！");
        }
    }

    @GetMapping("/get-by-permission")
    public R<List<User>> getUserByPermission(@RequestParam List<String> permissions,
                                             @RequestParam(value = "orderId", required = false) String orderId) {
        return R.ok(btoUserService.getUsersByPermission(permissions, orderId));
    }




}
